<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是shell——对SD卡等存储介质进行格式化、分区、挂载等相关命令学习的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV02-02-shell-09-存储介质管理命令">
<meta property="og:url" content="https://sumumm.github.io/post/afe014f2.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是shell——对SD卡等存储介质进行格式化、分区、挂载等相关命令学习的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240404092433146.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240404092555682.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240404092805220.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240404092909356.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240404093018327.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240404100834826.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240404100938591.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240404101533576.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405092811212.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405093057666.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405093946640.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405094039709.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405094307185.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405094718523.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405094817541.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405095140806.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405120929894.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405121943455.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405122257021.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405122355648.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405205414484.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405210138125.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405211035347.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405211224551.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405212156931.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405212331815.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405213053593.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405213246965.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405213743850.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240406080752832.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240406085255966.png">
<meta property="article:published_time" content="2024-04-06T02:38:47.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:56.988Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV02-编译基础">
<meta property="article:tag" content="shell脚本">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240404092433146.png">


<link rel="canonical" href="https://sumumm.github.io/post/afe014f2.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":"","permalink":"https://sumumm.github.io/post/afe014f2.html","path":"post/afe014f2.html","title":"LV02-02-shell-09-存储介质管理命令"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV02-02-shell-09-存储介质管理命令 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81lsblk"><span class="nav-text">一、lsblk</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E"><span class="nav-text">1. 命令说明</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">2. 使用格式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%B8%B8%E7%94%A8%E9%80%89%E9%A1%B9"><span class="nav-text">3. 常用选项</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-%E6%9F%A5%E7%9C%8B%E5%91%BD%E4%BB%A4%E7%89%88%E6%9C%AC"><span class="nav-text">3.1 查看命令版本</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E5%88%97%E8%A1%A8%E6%89%80%E6%9C%89%E5%9D%97%E8%AE%BE%E5%A4%87"><span class="nav-text">3.2 列表所有块设备</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-%E6%9F%A5%E7%9C%8B%E6%8C%87%E5%AE%9A%E5%9D%97%E8%AE%BE%E5%A4%87%E4%BF%A1%E6%81%AF"><span class="nav-text">3.3 查看指定块设备信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-%E6%9F%A5%E7%9C%8Bscsi%E4%BF%A1%E6%81%AF"><span class="nav-text">3.4 查看scsi信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-5-%E6%9F%A5%E7%9C%8B%E5%9D%97%E8%AE%BE%E5%A4%87%E7%9A%84%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E7%B1%BB%E5%9E%8B%E5%8F%8AUUID"><span class="nav-text">3.5 查看块设备的文件系统类型及UUID</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81mount-umount"><span class="nav-text">二、mount&#x2F;umount</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-mount"><span class="nav-text">1. mount</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E"><span class="nav-text">1.1 命令说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">1.2 使用格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E5%B8%B8%E7%94%A8%E9%80%89%E9%A1%B9"><span class="nav-text">1.3 常用选项</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-unmount"><span class="nav-text">2. unmount</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E"><span class="nav-text">2.1 命令说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">2.2 使用格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E5%B8%B8%E7%94%A8%E9%80%89%E9%A1%B9"><span class="nav-text">2.3 常用选项</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81df-du%E5%91%BD%E4%BB%A4"><span class="nav-text">三、df&#x2F;du命令</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-df"><span class="nav-text">1. df</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E-1"><span class="nav-text">1.1 命令说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F-1"><span class="nav-text">1.2 使用格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E5%B8%B8%E7%94%A8%E9%80%89%E9%A1%B9-1"><span class="nav-text">1.3 常用选项</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-1-%E6%9F%A5%E7%9C%8B%E7%A3%81%E7%9B%98%E4%BF%A1%E6%81%AF"><span class="nav-text">1.3.1 查看磁盘信息</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-2-%E6%9F%A5%E7%9C%8B%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F"><span class="nav-text">1.3.2 查看文件系统</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-du"><span class="nav-text">2. du</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E-1"><span class="nav-text">2.1 命令说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F-1"><span class="nav-text">2.2 使用格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E5%B8%B8%E7%94%A8%E9%80%89%E9%A1%B9-1"><span class="nav-text">2.3 常用选项</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81mkfs"><span class="nav-text">四、mkfs</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E-1"><span class="nav-text">1. 命令说明</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F-1"><span class="nav-text">2. 使用格式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2. 使用实例</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%94%E3%80%81fdisk"><span class="nav-text">五、fdisk</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E-2"><span class="nav-text">1. 命令说明</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%A3%81%E7%9B%98%E5%88%86%E5%8C%BA%E7%BB%93%E6%9E%84"><span class="nav-text">2. 磁盘分区结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">3. 使用格式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-fdisk%E5%91%BD%E4%BB%A4%E7%AA%97%E5%8F%A3"><span class="nav-text">4. fdisk命令窗口</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">5. 使用实例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-%E6%9F%A5%E7%9C%8B%E5%88%86%E5%8C%BA%E6%83%85%E5%86%B5"><span class="nav-text">5.1 查看分区情况</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2-%E8%BF%9B%E5%85%A5%E5%88%86%E5%8C%BA%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4%E7%AA%97%E5%8F%A3"><span class="nav-text">5.2 进入分区管理命令窗口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-3-%E5%88%A0%E9%99%A4%E5%88%86%E5%8C%BA"><span class="nav-text">5.3 删除分区</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-4-%E5%88%9B%E5%BB%BA%E5%88%86%E5%8C%BA"><span class="nav-text">5.4 创建分区</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%AD%E3%80%81%E8%87%AA%E5%8A%A8%E6%8C%82%E8%BD%BD%E5%88%86%E5%8C%BA"><span class="nav-text">六、自动挂载分区</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%83%E3%80%81%E5%86%99%E4%B8%80%E4%B8%AA%E8%84%9A%E6%9C%AC%E5%AE%9E%E7%8E%B0%E5%88%86%E5%8C%BA%EF%BC%9F"><span class="nav-text">七、写一个脚本实现分区？</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-text">1. | 运算符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-dd%E5%91%BD%E4%BB%A4"><span class="nav-text">2. dd命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-shell%E6%A0%87%E7%AD%BE"><span class="nav-text">3. shell标签</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E8%84%9A%E6%9C%AC%E5%AE%9E%E7%8E%B0%E5%88%86%E5%8C%BA"><span class="nav-text">4. 脚本实现分区</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/afe014f2.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV02-02-shell-09-存储介质管理命令 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV02-02-shell-09-存储介质管理命令
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2024-04-06 10:38:47" itemprop="dateCreated datePublished" datetime="2024-04-06T10:38:47+08:00">2024-04-06</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/" itemprop="url" rel="index"><span itemprop="name">LV02-编译基础</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>11k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>40 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是shell——对SD卡等存储介质进行格式化、分区、挂载等相关命令学习的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">---</td><td align="left"><a href="" target="_blank">--- <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<p>本文主要以SD为存储介质来学习这些命令。格式化SD卡是一种重要的维护操作，它可以清除存储介质上的所有数据，并为其创建一个新的文件系统。Linux提供了多种命令来格式化SD卡linux格式化sd卡命令，下面将详细学习这些命令及其用法。</p>
<h1 id="一、lsblk"><a href="#一、lsblk" class="headerlink" title="一、lsblk"></a><font size=3>一、lsblk</font></h1><h2 id="1-命令说明"><a href="#1-命令说明" class="headerlink" title="1. 命令说明"></a><font size=3>1. 命令说明</font></h2><p>lsblk命令的英文是“list block”，即用于列出所有可用块设备的信息，而且还能显示他们之间的依赖关系，但是它不会列出RAM盘的信息。块设备有硬盘，闪存盘，CD-ROM等等。lsblk命令包含util-linux中。通过yum provides lsblk命令查看命令对应的软件包。</p>
<h2 id="2-使用格式"><a href="#2-使用格式" class="headerlink" title="2. 使用格式"></a><font size=3>2. 使用格式</font></h2><p>我们使用man命令看一下使用格式：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">NAME</span><br><span class="line">       lsblk - list block devices</span><br><span class="line"></span><br><span class="line">SYNOPSIS</span><br><span class="line">       lsblk [options] [device...]</span><br></pre></td></tr></table></figure>

<p>可以看到这个命令主要是列出快设备信息，后面有一些参数，具体可以看man手册，后面只学习一些常用的选项。</p>
<h2 id="3-常用选项"><a href="#3-常用选项" class="headerlink" title="3. 常用选项"></a><font size=3>3. 常用选项</font></h2><h3 id="3-1-查看命令版本"><a href="#3-1-查看命令版本" class="headerlink" title="3.1 查看命令版本"></a><font size=3>3.1 查看命令版本</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lsblk -V</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240404092433146.png" alt="image-20240404092433146" style="zoom:50%;" />

<h3 id="3-2-列表所有块设备"><a href="#3-2-列表所有块设备" class="headerlink" title="3.2 列表所有块设备"></a><font size=3>3.2 列表所有块设备</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lsblk -a</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240404092555682.png" alt="image-20240404092555682" style="zoom:50%;" />

<p>可以看到列出了当前系统的所有块设备，包括分区信息，由于我之前SD卡烧写过正点原子出厂的系统，所以带有两个分区。</p>
<h3 id="3-3-查看指定块设备信息"><a href="#3-3-查看指定块设备信息" class="headerlink" title="3.3 查看指定块设备信息"></a><font size=3>3.3 查看指定块设备信息</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lsblk /dev/sdc</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240404092805220.png" alt="image-20240404092805220" style="zoom:50%;" />

<h3 id="3-4-查看scsi信息"><a href="#3-4-查看scsi信息" class="headerlink" title="3.4 查看scsi信息"></a><font size=3>3.4 查看scsi信息</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lsblk -S</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240404092909356.png" alt="image-20240404092909356" style="zoom:50%;" />

<h3 id="3-5-查看块设备的文件系统类型及UUID"><a href="#3-5-查看块设备的文件系统类型及UUID" class="headerlink" title="3.5 查看块设备的文件系统类型及UUID"></a><font size=3>3.5 查看块设备的文件系统类型及UUID</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lsblk -f</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240404093018327.png" alt="image-20240404093018327" style="zoom:50%;" />

<h1 id="二、mount-umount"><a href="#二、mount-umount" class="headerlink" title="二、mount&#x2F;umount"></a><font size=3>二、mount&#x2F;umount</font></h1><p>我们要是想查看SD卡分区里面的内容怎么办？这时候我们需要用到mount和umount命令。</p>
<p>需要知道的是Linux中的根目录以外的文件要想被访问，需要将其“关联”到根目录下的某个目录来实现，这种关联操作就是“挂载”，这个目录就是“挂载点”，解除次关联关系的过程称之为“卸载”。注意：“挂载点”的目录需要以下几个要求：</p>
<p>（1）目录事先存在，可以用mkdir命令新建目录；</p>
<p>（2）挂载点目录不可被其他进程使用到；</p>
<p>（3）挂载点下原有文件将被隐藏。</p>
<h2 id="1-mount"><a href="#1-mount" class="headerlink" title="1. mount"></a><font size=3>1. mount</font></h2><h3 id="1-1-命令说明"><a href="#1-1-命令说明" class="headerlink" title="1.1 命令说明"></a><font size=3>1.1 命令说明</font></h3><p>Linux的mount命令用于挂载Linux系统外的文件。这个命令允许用户将额外的子文件系统挂载到当前可访问文件系统的特定挂载点。命令将挂载指令传递给内核，由内核完成操作。这个命令允许我们访问和管理各种不同的文件系统和设备，包括硬盘驱动器、USB设备、网络文件系统（NFS）等。</p>
<h3 id="1-2-使用格式"><a href="#1-2-使用格式" class="headerlink" title="1.2 使用格式"></a><font size=3>1.2 使用格式</font></h3><p>我们可以使用man命令来查看一下帮助手册：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">NAME</span><br><span class="line">       mount - mount a filesystem</span><br><span class="line"></span><br><span class="line">SYNOPSIS</span><br><span class="line">       mount [-l|-h|-V]</span><br><span class="line"></span><br><span class="line">       mount -a [-fFnrsvw] [-t fstype] [-O optlist]</span><br><span class="line"></span><br><span class="line">       mount [-fnrsvw] [-o options] device|dir</span><br><span class="line"></span><br><span class="line">       mount [-fnrsvw] [-t fstype] [-o options] device dir</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>可以看到还是有挺多格式和选项的，接下来通过实例来学习一些常用的选项。</p>
<h3 id="1-3-常用选项"><a href="#1-3-常用选项" class="headerlink" title="1.3 常用选项"></a><font size=3>1.3 常用选项</font></h3><p>这里其实使用的选项不多，我们一般使用最基础的就可以了，我们可以使用如下的命令进行挂载</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mkdir -p ~/temp/sd # 创建挂载点</span><br><span class="line">sudo mount /dev/sdc1 ~/temp/sd</span><br></pre></td></tr></table></figure>

<p>需要注意的是，这里我们需要加上sudo，另外由于SD卡被分分区了，所以我们直接挂载&#x2F;dev&#x2F;sdc的时候会提示设备繁忙：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240404100834826.png" alt="image-20240404100834826" style="zoom:50%;" />

<p>这样的话，我们直接挂载分区好了：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240404100938591.png" alt="image-20240404100938591" style="zoom:50%;" />

<p>可以看到挂载成功后，我们可以通过~&#x2F;temp&#x2F;sd目录来查看&#x2F;dev&#x2F;sdc1分区中的所有文件</p>
<h2 id="2-unmount"><a href="#2-unmount" class="headerlink" title="2. unmount"></a><font size=3>2. unmount</font></h2><h3 id="2-1-命令说明"><a href="#2-1-命令说明" class="headerlink" title="2.1 命令说明"></a><font size=3>2.1 命令说明</font></h3><p>Linux umount（英文全拼：unmount）命令用于卸除文件系统。它可卸除目前挂在Linux目录中的文件系统。</p>
<h3 id="2-2-使用格式"><a href="#2-2-使用格式" class="headerlink" title="2.2 使用格式"></a><font size=3>2.2 使用格式</font></h3><p>还是man手册：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">NAME</span><br><span class="line">       umount - unmount file systems</span><br><span class="line"></span><br><span class="line">SYNOPSIS</span><br><span class="line">       umount -a [-dflnrv] [-t fstype] [-O option...]</span><br><span class="line"></span><br><span class="line">       umount [-dflnrv] &#123;directory|device&#125;...</span><br><span class="line"></span><br><span class="line">       umount -h|-V</span><br></pre></td></tr></table></figure>

<p>详细的选项及含义可以查看手册。</p>
<h3 id="2-3-常用选项"><a href="#2-3-常用选项" class="headerlink" title="2.3 常用选项"></a><font size=3>2.3 常用选项</font></h3><p>这里其实我们也只是常用最基本的命令，上面我们已经把&#x2F;dev&#x2F;sdc1挂载到~&#x2F;temp&#x2F;sd目录了，我们先可以通过下面的命令来卸载：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo umount -v /dev/sdc1        # 通过设备名卸载</span><br><span class="line">sudo umount -v ~/temp/sdc1      # 通过挂载点卸载</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240404101533576.png" alt="image-20240404101533576" style="zoom:50%;" />

<p>需要注意的是这个命令需要加上sudo。</p>
<h1 id="三、df-du命令"><a href="#三、df-du命令" class="headerlink" title="三、df&#x2F;du命令"></a><font size=3>三、df&#x2F;du命令</font></h1><p>这里需要再了解两个命令，就是df 和 du 命令，它俩是Linux 查看磁盘空间使用的。</p>
<h2 id="1-df"><a href="#1-df" class="headerlink" title="1. df"></a><font size=3>1. df</font></h2><h3 id="1-1-命令说明-1"><a href="#1-1-命令说明-1" class="headerlink" title="1.1 命令说明"></a><font size=3>1.1 命令说明</font></h3><p><code>df</code>，disk free。该 命令用于显示目前在 Linux 系统上的文件系统磁盘使用情况统计。也就是以磁盘分区为单位查看文件系统，可以获取硬盘被占用了多少空间，目前还剩下多少空间等信息。需要注意的是，它只能看已经挂载的磁盘的信息，没挂载的要先挂载。</p>
<h3 id="1-2-使用格式-1"><a href="#1-2-使用格式-1" class="headerlink" title="1.2 使用格式"></a><font size=3>1.2 使用格式</font></h3><p>我们还是来看man手册：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">NAME</span><br><span class="line">       df - report file system disk space usage</span><br><span class="line"></span><br><span class="line">SYNOPSIS</span><br><span class="line">       df [OPTION]... [FILE]...</span><br></pre></td></tr></table></figure>

<p>它也有很多选项，但是一般都用不到，后面通过实例来学习一些常用选项。</p>
<details class="folding-tag" blue><summary> 点击查看选项说明 </summary>
              <div class='content'>
              <figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">-a或--all：包含全部的文件系统；</span><br><span class="line">--block-size=&lt;区块大小&gt;：以指定的区块大小来显示区块数目；</span><br><span class="line">-h或--human-readable：以可读性较高的方式来显示信息；</span><br><span class="line">-H或--si：与-h参数相同，但在计算时是以1000 Bytes为换算单位而非1024 Bytes；</span><br><span class="line">-i或--inodes：显示inode的信息；</span><br><span class="line">-k或--kilobytes：指定区块大小为1024字节；</span><br><span class="line">-l或--local：仅显示本地端的文件系统；</span><br><span class="line">-m或--megabytes：指定区块大小为1048576字节；</span><br><span class="line">--no-sync：在取得磁盘使用信息前，不要执行sync指令，此为预设值；</span><br><span class="line">-P或--portability：使用POSIX的输出格式；</span><br><span class="line">--sync：在取得磁盘使用信息前，先执行sync指令；</span><br><span class="line">-t&lt;文件系统类型&gt;或--type=&lt;文件系统类型&gt;：仅显示指定文件系统类型的磁盘信息；</span><br><span class="line">-T或--print-type：显示文件系统的类型；</span><br><span class="line">-x&lt;文件系统类型&gt;或--exclude-type=&lt;文件系统类型&gt;：不要显示指定文件系统类型的磁盘信息；</span><br><span class="line">--help：显示帮助；</span><br><span class="line">--version：显示版本信息。</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="1-3-常用选项-1"><a href="#1-3-常用选项-1" class="headerlink" title="1.3 常用选项"></a><font size=3>1.3 常用选项</font></h3><h4 id="1-3-1-查看磁盘信息"><a href="#1-3-1-查看磁盘信息" class="headerlink" title="1.3.1 查看磁盘信息"></a><font size=3>1.3.1 查看磁盘信息</font></h4><p>一般我们使用-h选项来查看磁盘信息就够了：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">df -h</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405092811212.png" alt="image-20240405092811212" style="zoom:50%;" />

<table>
<thead>
<tr>
<th>内容参数</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td><code>Filesystem</code></td>
<td>文件系统</td>
</tr>
<tr>
<td><code>Size</code></td>
<td>分区大小</td>
</tr>
<tr>
<td><code>1k-blocks</code></td>
<td>单位是1KB(使用<code>df</code>查看，不加选项)</td>
</tr>
<tr>
<td><code>Used</code></td>
<td>已用容量</td>
</tr>
<tr>
<td><code>Avail</code></td>
<td>还可用的容量</td>
</tr>
<tr>
<td><code>Use%</code></td>
<td>已用百分比</td>
</tr>
<tr>
<td><code>Mounted on</code></td>
<td>挂载点</td>
</tr>
</tbody></table>
<h4 id="1-3-2-查看文件系统"><a href="#1-3-2-查看文件系统" class="headerlink" title="1.3.2 查看文件系统"></a><font size=3>1.3.2 查看文件系统</font></h4><p>我们有时候想看一下磁盘文件系统是什么怎么办？就比如有时候想要看一下SD卡的文件系统，我们可以使用-T选项：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">df -T</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405093057666.png" alt="image-20240405093057666" style="zoom:48%;" />

<p>这样就可以看到我们之前的SD卡上两个分区的文件系统格式分别为vfat和ext3.</p>
<h2 id="2-du"><a href="#2-du" class="headerlink" title="2. du"></a><font size=3>2. du</font></h2><h3 id="2-1-命令说明-1"><a href="#2-1-命令说明-1" class="headerlink" title="2.1 命令说明"></a><font size=3>2.1 命令说明</font></h3><p>du命令用来查看目录或文件所占用磁盘空间的大小。</p>
<h3 id="2-2-使用格式-1"><a href="#2-2-使用格式-1" class="headerlink" title="2.2 使用格式"></a><font size=3>2.2 使用格式</font></h3><p>看一下man手册吧：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">NAME</span><br><span class="line">       du - estimate file space usage</span><br><span class="line"></span><br><span class="line">SYNOPSIS</span><br><span class="line">       du [OPTION]... [FILE]...</span><br><span class="line">       du [OPTION]... --files0-from=F</span><br><span class="line"></span><br><span class="line">DESCRIPTION</span><br><span class="line">       Summarize disk usage of the set of FILEs, recursively for directories.</span><br></pre></td></tr></table></figure>

<p>看这个可能更清晰一点吧：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">du [-abcDhHklmsSx][-L &lt;符号连接&gt;][-X &lt;文件&gt;][--block-size][--exclude=&lt;目录或文件&gt;][--max-depth=&lt;目录层数&gt;][--help][--version][目录或文件]</span><br></pre></td></tr></table></figure>

<details class="folding-tag" blue><summary> 点击查看参数说明 </summary>
              <div class='content'>
              <p>参数说明：</p><ul><li>-a或-all 显示目录中个别文件的大小。</li><li>-b或-bytes 显示目录或文件大小时，以byte为单位。</li><li>-c或–total 除了显示个别目录或文件的大小外，同时也显示所有目录或文件的总和。</li><li>-D或–dereference-args 显示指定符号连接的源文件大小。</li><li>-h或–human-readable 以K，M，G为单位，提高信息的可读性。</li><li>-H或–si 与-h参数相同，但是K，M，G是以1000为换算单位。</li><li>-k或–kilobytes 以1024 bytes为单位。</li><li>-l或–count-links 重复计算硬件连接的文件。</li><li>-L&lt;符号连接&gt;或–dereference&lt;符号连接&gt; 显示选项中所指定符号连接的源文件大小。</li><li>-m或–megabytes 以1MB为单位。</li><li>-s或–summarize 仅显示指定目录或文件的总大小，而不显示其子目录的大小。</li><li>-S或–separate-dirs 显示个别目录的大小时，并不含其子目录的大小。</li><li>-x或–one-file-xystem 以一开始处理时的文件系统为准，若遇上其它不同的文件系统目录则略过。</li><li>-X&lt;文件&gt;或–exclude-from&#x3D;&lt;文件&gt; 在&lt;文件&gt;指定目录或文件。</li><li>–exclude&#x3D;&lt;目录或文件&gt; 略过指定的目录或文件。</li><li>–max-depth&#x3D;&lt;目录层数&gt; 超过指定层数的目录后，予以忽略。</li><li>–help 显示帮助。</li><li>–version 显示版本信息。</li></ul>
              </div>
            </details>

<h3 id="2-3-常用选项-1"><a href="#2-3-常用选项-1" class="headerlink" title="2.3 常用选项"></a><font size=3>2.3 常用选项</font></h3><ul>
<li>查看当前目录总共占的容量。而不单独列出各子项占用的容量</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">du -sh</span><br></pre></td></tr></table></figure>



<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405093946640.png" alt="image-20240405093946640" style="zoom:50%;" />

<ul>
<li>查看当前目录下一级子文件和子目录占用的磁盘容量。</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">du -lh --max-depth=1</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405094039709.png" alt="image-20240405094039709" style="zoom:50%;" />



<h1 id="四、mkfs"><a href="#四、mkfs" class="headerlink" title="四、mkfs"></a><font size=3>四、mkfs</font></h1><p>前面我们已经学习了如何查看SD卡、如何挂载和卸载SD卡。在windows下，我们识别到sd卡后，直接右键然后格式化就可以，那么linux下怎么办？这个时候就要用到mkfs命令啦。</p>
<h2 id="1-命令说明-1"><a href="#1-命令说明-1" class="headerlink" title="1. 命令说明"></a><font size=3>1. 命令说明</font></h2><p>mkfs，英文全称是 “make file system”，该命令主要用于在特定的分区上创建Linux文件系统。此命令初始化卷标签、文件系统标签和启动块。 简单来说，<code>mkfs</code> 是用来格式化分区，创建文件系统的。</p>
<h2 id="2-使用格式-1"><a href="#2-使用格式-1" class="headerlink" title="2. 使用格式"></a><font size=3>2. 使用格式</font></h2><p>我们可以使用man命令来查看一下帮助手册：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">NAME</span><br><span class="line">       mkfs - build a Linux filesystem</span><br><span class="line"></span><br><span class="line">SYNOPSIS</span><br><span class="line">       mkfs [options] [-t type] [fs-options] device [size]</span><br><span class="line"></span><br><span class="line">DESCRIPTION</span><br><span class="line">       This  mkfs  frontend  is  deprecated  in  favour of filesystem specific</span><br><span class="line">       mkfs.&lt;type&gt; utils.</span><br></pre></td></tr></table></figure>

<p>具体的选项含义还是可以看man手册，说的都比较详细。这里有一个参数 -t ，这个参数man手册是这样说的：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">-t, --type type</span><br><span class="line">              Specify  the  type of filesystem to be built.  If not specified,</span><br><span class="line">              the default filesystem type (currently ext2) is used.</span><br></pre></td></tr></table></figure>

<p>翻译一下就是指定要构建的文件系统类型。如果未指定，则使用默认的文件系统类型(目前为ext2)。我们在平时使用的时候会遇到这样的命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">mkfs.vfat # 创建FAT32文件系统</span><br><span class="line">mkfs.ntfs # 创建NTFS文件系统</span><br><span class="line">mkfs.ext4</span><br></pre></td></tr></table></figure>

<p>这些其实与下面这样写是一样的：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">mkfs -t vfat # 创建FAT32文件系统</span><br><span class="line">mkfs -t ntfs</span><br><span class="line">mkfs -t ext4</span><br></pre></td></tr></table></figure>

<p>【注意】这个命令使用的时候需要root权限。</p>
<h2 id="2-使用实例"><a href="#2-使用实例" class="headerlink" title="2. 使用实例"></a><font size=3>2. 使用实例</font></h2><p>我们前面使用的SD卡有两个分区，一个是sdc1一个是sdc2，它们的文件格式分别为：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405094307185.png" alt="image-20240405094307185" style="zoom:50%;" />

<p>接下来我们将sdc1分区格式化为ext4：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo mkfs -t ext4 /dev/sdc1</span><br></pre></td></tr></table></figure>

<p>需要注意的是，如果该设备已经有一个文件系统了，<code>mkfs </code>命令会给出错误消息（我看实际操作是给出了询问）。在此种情况下，如果确定要在设备上创建新的文件系统，可以添加 <code>-F</code> 选项来强制执行操作，或者是在交互界面选择y。但是可能还会有一个问题，就是如果这个分区被挂载到系统中，就会格式化失败：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405094718523.png" alt="image-20240405094718523" style="zoom:50%;" />

<p>所以我们要先卸载挂载点，然后再执行对应的命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo umount /dev/sdc1</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405094817541.png" alt="image-20240405094817541" style="zoom:50%;" />

<p>然后我们先挂载，再使用df命令来看一下现在的文件系统情况：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">mkdir -p ~/temp/sd_temp</span><br><span class="line">sudo mount /dev/sdc1 ~/temp/sd_temp</span><br><span class="line">df -T</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405095140806.png" alt="image-20240405095140806" style="zoom:45%;" />

<p>可以看到已经被格式化为ext4格式了，这个时候这个分区的文件已经被全部清空，所以格式化前一定要注意备份重要文件。</p>
<h1 id="五、fdisk"><a href="#五、fdisk" class="headerlink" title="五、fdisk"></a><font size=3>五、fdisk</font></h1><p>我们再来学习一个命令，就是fdisk命令，这个命令很强大，上面我们知道了如何挂载、卸载、查看、格式化SD卡，但是如何对sd卡进行分区？这个时候就要用到它啦。</p>
<h2 id="1-命令说明-2"><a href="#1-命令说明-2" class="headerlink" title="1. 命令说明"></a><font size=3>1. 命令说明</font></h2><p>Linux fdisk 是一个创建和维护分区表的程序，它兼容 DOS 类型的分区表、BSD 或者 SUN 类型的磁盘列表。</p>
<h2 id="2-磁盘分区结构"><a href="#2-磁盘分区结构" class="headerlink" title="2. 磁盘分区结构"></a><font size=3>2. 磁盘分区结构</font></h2><p>先来了解点命令之外的东西——磁盘分区结构。磁盘的分区结构有两种，一种是MBR分区，一种是GPT分区。</p>
<ul>
<li>MBR(Master Boot Record)</li>
</ul>
<p>是传统的分区机制，应用于绝大多数使用BIOS引导的PC设备（苹果使用EFI的方式），很多Server服务器即支持BIOS也支持EFI的引导方式；MBR支持32bit和64bit系统；MBR分区分为主分区（一块硬盘最多只能创建4个主分区）、扩展分区（一个扩展分区会占用一个主分区的位置）、逻辑分区（逻辑分区是基于扩展分区创建出来的，先有扩展分区，然后在扩展分区的基础上再创建逻辑分区；也就是说我们要使用逻辑分区，必须先要创建扩展分区，扩展分区的空间是不能被直接使用的，我们必须在扩展分区的基础上去建立逻辑分区，才能够被使用）。在Linux上使用扩展分区和逻辑分区最多可以创建15个分区；MBR只支持不超过2TB的硬盘。</p>
<ul>
<li>GPT(GUID Partition Table)</li>
</ul>
<p>是一个较新的分区机制，解决了MBR的很多缺点；持超过2TB的磁盘；向后兼容MBR；使用GPT分区的前提是底层硬件必须支持UEFI（Intel提出的取代BIOS的新一代的引导系统）才能使用，也就是底层硬件必须使用UEFI；GPT分区只支持64位操作系统；GPT分区列表支持最大128PB (1PB &#x3D; 1024TB)，可以定义128个分区，没有主分区，扩展分区和逻辑分区的概念，所有分区都能格式化。</p>
<h2 id="3-使用格式"><a href="#3-使用格式" class="headerlink" title="3. 使用格式"></a><font size=3>3. 使用格式</font></h2><p>对于fdisk命令，功能还是很强大的，有很多的参数选项，我们看一下man手册，了解下它的使用格式：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">NAME</span><br><span class="line">       fdisk - manipulate disk partition table</span><br><span class="line"></span><br><span class="line">SYNOPSIS</span><br><span class="line">       fdisk [options] device</span><br><span class="line"></span><br><span class="line">       fdisk -l [device...]</span><br></pre></td></tr></table></figure>

<p>它有很多选项，我们后边通过实例来学习。</p>
<details class="folding-tag" blue><summary> 点击查看选项说明 </summary>
              <div class='content'>
              <table><tr><td style="width:152px;">参数/选项（缩写）</td><td style="width:175px;">参数/选项（完整意思）</td><td style="width:361px;">使用说明/解释</td></tr><tr><td style="width:152px;">-b</td><td style="width:175px;">--扇区大小扇区大小</td><td style="width:361px;"> <p>指定磁盘的扇区大小。有效值为512、1024、2048和4096。（最近的内核知道扇区大小。只在旧内核上使用此选项，或者覆盖内核的想法。）由于util-linux-2.17，fdisk区分逻辑和物理扇区大小。此选项将两个扇区大小都更改为扇区大小。</p> </td></tr><tr><td style="width:152px;">-B</td><td style="width:175px;">--保护启动</td><td style="width:361px;"> <p>创建新磁盘标签时，不要擦除第一个磁盘扇区的开头。GPT和MBR支持此功能。</p> </td></tr><tr><td style="width:152px;">-c</td><td style="width:175px;">--兼容性[=模式]</td><td style="width:361px;"> <p>指定兼容模式“dos”或“nondos”。默认为非DOS模式。为了向后兼容，可以使用不带模式参数的选项，然后使用默认值。请注意，可选模式参数不能与-c选项用空格分隔，正确的形式是例如“-c=dos”。</p> </td></tr><tr><td style="width:152px;">-h</td><td style="width:175px;">--帮助</td><td style="width:361px;"> <p>显示帮助文本并退出。</p> </td></tr><tr><td style="width:152px;"> <p>-L</p> </td><td style="width:175px;"> <p>--color[=何时]</p> </td><td style="width:361px;">将输出着色。可选参数when可以是auto、never或always。如果省略了when参数，则默认为auto。可以禁用颜色；有关当前内置默认值，请参阅--help输出。另请参见颜色部分。</td></tr><tr><td style="width:152px;">-l</td><td style="width:175px;">--列表</td><td style="width:361px;"> <p>列出指定设备的分区表，然后退出。如果没有给出任何设备，则使用/proc/partitions中提到的设备（如果该文件存在）。</p> </td></tr><tr><td style="width:152px;">-o</td><td style="width:175px;">--输出列表</td><td style="width:361px;"> <p>指定要打印的输出列。使用--help获取所有支持列的列表。如果列表的格式为+列表（例如-o+UUID），则可以扩展默认列列表。</p> </td></tr><tr><td style="width:152px;"> <p>-s</p> </td><td style="width:175px;"> <p>&nbsp;--获取</p> </td><td style="width:361px;">打印每个给定块设备的512字节扇区的大小。此选项已弃用，以支持blockdev（1）。</td></tr><tr><td style="width:152px;"> <p>-t</p> </td><td style="width:175px;"> <p>&nbsp;--类型类型</p> </td><td style="width:361px;">仅对指定类型的磁盘标签启用支持，并禁用对所有其他类型的支持。</td></tr><tr><td style="width:152px;">-u</td><td style="width:175px;">--单位[=单位]</td><td style="width:361px;"> <p>列出分区表时，以“扇区”或“柱面”显示大小。默认情况是以扇区显示大小。为了向后兼容，可以使用不带单位参数的选项，然后使用默认值。请注意，可选的单位参数不能与-u选项用空格分隔，正确的形式是例如“-u=cylinders”。</p> </td></tr><tr><td style="width:152px;">-C</td><td style="width:175px;">--气缸数量</td><td style="width:361px;"> <p>指定磁盘的气缸数。我不知道为什么会有人想这么做。</p> </td></tr><tr><td style="width:152px;">-H</td><td style="width:175px;"> <p>--头数</p> </td><td style="width:361px;">指定磁盘的磁头数。（当然不是物理数字，而是用于分区表的数字。）合理的值是255和16。</td></tr><tr><td style="width:152px;">-S</td><td style="width:175px;">--扇区编号</td><td style="width:361px;"> <p>指定磁盘每个磁道的扇区数。（当然不是物理数字，而是用于分区表的数字。）合理的值是63</p> </td></tr><tr><td style="width:152px;">-w</td><td style="width:175px;">--擦拭时</td><td style="width:361px;"> <p>从设备中擦除文件系统、RAID和分区表签名，以避免可能的冲突。参数when可以是auto、never或always。如果未给定此选项，则默认为“自动”，在这种情况下，只有在交互模式下才会擦除签名。在所有情况下，在创建新分区表之前，检测到的签名都会通过警告消息进行报告。另请参见wipefs（8）命令。</p> </td></tr><tr><td style="width:152px;">-W</td><td style="width:175px;">--擦除分区时</td><td style="width:361px;"> <p>从新创建的分区中擦除文件系统、RAID和分区表签名，以避免可能的冲突。参数when可以是auto、never或always。如果未给定此选项，则默认为“自动”，在这种情况下，只有在交互模式下以及用户确认后，才会擦除签名。在所有情况下，在创建新分区之前，检测到的签名都会通过警告消息进行报告。另请参见wipefs（8）命令。</p> </td></tr><tr><td style="width:152px;">-V</td><td style="width:175px;">--版本</td><td style="width:361px;"> <p>显示版本信息并退出。</p> </td></tr></table>
              </div>
            </details>

<h2 id="4-fdisk命令窗口"><a href="#4-fdisk命令窗口" class="headerlink" title="4. fdisk命令窗口"></a><font size=3>4. fdisk命令窗口</font></h2><p>这个命令是有自己的命令窗口的，我们使用fdisk命令进入命令窗口：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo fdisk /dev/sdx</span><br></pre></td></tr></table></figure>

<p>其中sdx表示磁盘节点，我这里是SD卡，插上后节点为sdc：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405120929894.png" alt="image-20240405120929894" style="zoom:50%;" />

<details class="folding-tag" blue><summary> 点击查看命令说明 </summary>
              <div class='content'>
              <figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">a   toggle a bootable flag                                   # 切换可启动标志</span><br><span class="line">b   edit bsd disklabel　　　　　　　　　　　　　　　　　　         # 编辑磁盘标记</span><br><span class="line">c   toggle the dos compatibility flag　　　　　　　　　　　　　　 # 切换dos兼容标志</span><br><span class="line"></span><br><span class="line">d   delete a partition　　　　　　　　　　　　　　　　　　　　　　　 # 删除一个分区</span><br><span class="line">F   list free unpartitioned space</span><br><span class="line">l   list known partition types　　　　　　　　　　　　　　　　　　　# 列出已知的分区类型　　　　　　　　　　　　　　　　　　</span><br><span class="line">n   add a new partition                                       # 添加一个分区</span><br><span class="line">p   print the partition table                                 # 打印分区表</span><br><span class="line">t   change a partition&#x27;s system id                            # 改变一个分区的类型</span><br><span class="line">v   verify the partition table                                # 验证分区表</span><br><span class="line">i   print information about a partition</span><br><span class="line"></span><br><span class="line">m   print this menu　　　　　　　　　　　　　　　　　　　　　　　　　　# 打印菜单</span><br><span class="line">u   change display/entry units                                # 改变显示的单位</span><br><span class="line">x   extra functionality (experts only)                        # 高级功能</span><br><span class="line"></span><br><span class="line">w   write table to disk and exit                              # 写分区表并退出</span><br><span class="line">q   quit without saving changes                               # 退出不保存</span><br><span class="line"></span><br><span class="line">g   create a new empty GPT partition table             　　   # 创建一个空的GPT分区表</span><br><span class="line">G   create an IRIX (SGI) partition table            　　      # 创建一个IRIX分区表</span><br><span class="line">o   create a new empty DOS partition table                    # 创建一个空的DOS分区表</span><br><span class="line">s   create a new empty Sun disklabel                          # 创建一个空的sun磁盘标签</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="5-使用实例"><a href="#5-使用实例" class="headerlink" title="5. 使用实例"></a><font size=3>5. 使用实例</font></h2><blockquote>
<p>说明：这一部分的终端标号说明如下：</p>
<p><code>hk@vm:~$</code> 表示在shell终端敲的命令</p>
<p><code>fdisk&gt;</code>表示在fdisk命令窗口里面敲的命令，实际是没有这个的吗，这里只是为了便于区分。</p>
</blockquote>
<h3 id="5-1-查看分区情况"><a href="#5-1-查看分区情况" class="headerlink" title="5.1 查看分区情况"></a><font size=3>5.1 查看分区情况</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hk@vm:~$ sudo fdisk -l</span><br></pre></td></tr></table></figure>

<p>然后会看到以下打印信息：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">hk@vm:~$ sudo fdisk -l</span><br><span class="line">Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors</span><br><span class="line">Units: sectors of 1 * 512 = 512 bytes</span><br><span class="line">Sector size (logical/physical): 512 bytes / 512 bytes</span><br><span class="line">I/O size (minimum/optimal): 512 bytes / 512 bytes</span><br><span class="line">Disklabel type: dos</span><br><span class="line">Disk identifier: 0x04609dd1</span><br><span class="line"></span><br><span class="line">设备       启动    Start   末尾   扇区  Size Id 类型</span><br><span class="line">/dev/sda1  *        2048 60913663 60911616   29G 83 Linux</span><br><span class="line">/dev/sda2       60915710 62912511  1996802  975M  5 扩展</span><br><span class="line">/dev/sda5       60915712 62912511  1996800  975M 82 Linux 交换 / Solaris</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Disk /dev/sdc: 3.7 GiB, 3965190144 bytes, 7744512 sectors</span><br><span class="line">Units: sectors of 1 * 512 = 512 bytes</span><br><span class="line">Sector size (logical/physical): 512 bytes / 512 bytes</span><br><span class="line">I/O size (minimum/optimal): 512 bytes / 512 bytes</span><br><span class="line">Disklabel type: dos</span><br><span class="line">Disk identifier: 0x66527a5d</span><br><span class="line"></span><br><span class="line">设备       启动  Start  末尾  扇区  Size Id 类型</span><br><span class="line">/dev/sdc1  *      2048  133119  131072   64M  c W95 FAT32 (LBA)</span><br><span class="line">/dev/sdc2       133120 7744511 7611392  3.6G 83 Linux</span><br></pre></td></tr></table></figure>

<p>想要单独看某一个磁盘呢？我们可直接用下面的命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hk@vm:~$ sudo fdisk -l /dev/sdc</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405121943455.png" alt="image-20240405121943455" style="zoom:50%;" />

<p>Disk label type: dos 这行信息说明该分区是MBR分区,如果显示Disk label type: gpt则说明该分区是GPT分区。</p>
<h3 id="5-2-进入分区管理命令窗口"><a href="#5-2-进入分区管理命令窗口" class="headerlink" title="5.2 进入分区管理命令窗口"></a><font size=3>5.2 进入分区管理命令窗口</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hk@vm:~$ sudo fdisk /dev/sdc</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405122257021.png" alt="image-20240405122257021" style="zoom: 50%;" />

<ul>
<li>查看当前分区表</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">fdisk&gt; </span><span class="language-bash">p</span></span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405122355648.png" alt="image-20240405122355648" style="zoom:50%;" />

<h3 id="5-3-删除分区"><a href="#5-3-删除分区" class="headerlink" title="5.3 删除分区"></a><font size=3>5.3 删除分区</font></h3><p>我们之前测试的时候SD卡有两个分区，我们将这两个分区删除，方便后面学习对应命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">fdisk&gt; </span><span class="language-bash">d <span class="comment"># 如果有多个就多次输入d</span></span></span><br></pre></td></tr></table></figure>

<p>由于之前有两个分区，所以操作是这样的：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405205414484.png" alt="image-20240405205414484" style="zoom: 50%;" />

<p>这样就删除了两个分区了。但是这个时候我们再开一个终端，会发现sdc1和sdc2都还在，也就是说我们虽然删除了分区，但是实际是没有生效的，如何生效？我们需要再使用w命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">fdisk&gt; </span><span class="language-bash">w</span></span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405210138125.png" alt="image-20240405210138125" style="zoom:50%;" />

<p>这就是由于我们之前挂载了sd卡两个分区的原因，所以我们要先试用umount来卸载这两个分区。然后再使用w命令即可。</p>
<h3 id="5-4-创建分区"><a href="#5-4-创建分区" class="headerlink" title="5.4 创建分区"></a><font size=3>5.4 创建分区</font></h3><p>接下来我们来重建两个分区。</p>
<ul>
<li>我们用命令 fdisk &#x2F;dev&#x2F;sda 进入分区操作，输入 p ，查看当前分区表，此时没有分区</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">hk@vm:~$ sudo fdisk /dev/sdc</span><br><span class="line"><span class="meta prompt_">fdisk&gt; </span><span class="language-bash">p</span></span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405211035347.png" alt="image-20240405211035347" style="zoom:50%;" />

<ul>
<li>创建分区</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">fdisk&gt; </span><span class="language-bash">n <span class="comment"># 新建一个分区</span></span></span><br><span class="line"><span class="meta prompt_">fdisk&gt; </span><span class="language-bash">p <span class="comment"># 选择分区类型为主分区（如果想添加扩展分区，就按e）</span></span></span><br><span class="line"><span class="meta prompt_">fdisk&gt; </span><span class="language-bash">1 <span class="comment"># 然后输入 1 ，选择分区号（这里注意一下，加上扩展分区，主分区只能有四个，所以只能选择1-4，一般按顺序输入分区号）</span></span></span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405211224551.png" alt="image-20240405211224551" style="zoom:50%;" />



<ul>
<li>确定分区信息</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">（1）然后会提示输入第一个扇区，这里直接回车默认2048就好</span><br><span class="line">（2）接着会让输入第一个分区的大小，这里是加了200M，输入+200M（不要忘记+号）；</span><br><span class="line">（3）网上的教程说分区大小选择好后，会包含一个签名，询问是否要移除该签名，如果不移除的话，此时的分区格式就是ntfs，这里我们需要ntfs格式的分区，所以不移除，输入 N。（但是我用的似乎没有这个，直接就分好了）</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405212156931.png" alt="image-20240405212156931" style="zoom:50%;" />

<ul>
<li>查看分区</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">fdisk&gt; </span><span class="language-bash">p</span></span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405212331815.png" alt="image-20240405212331815" style="zoom:50%;" />

<p>可以看到一个sdc1已经生成啦，而且是200MB的。</p>
<ul>
<li>保存退出</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">fdisk&gt; </span><span class="language-bash">w</span></span><br><span class="line"><span class="meta prompt_">fdisk&gt; </span><span class="language-bash">q</span></span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405213053593.png" alt="image-20240405213053593" style="zoom:50%;" />

<p>好像也不用输入q，然后我们看一下是不是有sdc1分区啦：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hk@vm:~$ sudo fdisk -l /dev/sdc</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405213246965.png" alt="image-20240405213246965" style="zoom:50%;" />

<ul>
<li>格式化分区</li>
</ul>
<p>我们已经创建好了分区，接下来就可以使用上面的mkfs命令来对分区进行格式化：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">hk@vm:~$ sudo mkfs.ext4 /dev/sdc1</span><br><span class="line">hk@vm:~$ sudo mkdir -p ~/temp/sd_temp</span><br><span class="line">hk@vm:~$ sudo mount /dev/sdc1 ~/temp/sd_temp</span><br><span class="line">hk@vm:~$ sudo df -hT</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240405213743850.png" alt="image-20240405213743850" style="zoom:50%;" />

<h1 id="六、自动挂载分区"><a href="#六、自动挂载分区" class="headerlink" title="六、自动挂载分区"></a><font size=3>六、自动挂载分区</font></h1><p>前面我们已经知道了如何查看分区，删除分区、新建分区，那么分区重建后可能是不会自动挂载的，我们可以使用以下命令来实现自动挂载：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;/dev/sdb1  /sdc1 ext4 defaults 0 0 &quot; &gt;&gt; /etc/fstab</span><br></pre></td></tr></table></figure>

<p>&#x2F;etc&#x2F;fstab文件详解：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">注释，/etc/fstab文件详解</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">示例为： /dev/sda3              /home         ext4      defaults,noatime      0      2</span></span><br><span class="line"></span><br><span class="line">/etc/fstab文件主要包括6段，依次是：</span><br><span class="line">&lt;file system&gt;　　&lt;dir&gt;　　&lt;type&gt;　　&lt;options&gt;　　&lt;dump&gt;　　&lt;pass&gt;</span><br><span class="line">&lt;file system&gt; 要挂载的分区或存储设备</span><br><span class="line">&lt;dir&gt;  挂载的目录位置</span><br><span class="line">&lt;type&gt; 挂载分区的文件系统类型，比如：ext3、ext4、xfs、swap</span><br><span class="line">&lt;options&gt; 挂载使用的参数有哪些。举例如下：</span><br><span class="line">	auto - 在启动时或键入了 mount -a 命令时自动挂载。</span><br><span class="line">	noauto - 只在你的命令下被挂载。</span><br><span class="line">	exec - 允许执行此分区的二进制文件。</span><br><span class="line">	noexec - 不允许执行此文件系统上的二进制文件。</span><br><span class="line">	ro - 以只读模式挂载文件系统。</span><br><span class="line">	rw - 以读写模式挂载文件系统。</span><br><span class="line">	user - 允许任意用户挂载此文件系统，若无显示定义，隐含启用 noexec, nosuid, nodev 参数。</span><br><span class="line">	users - 允许所有 users 组中的用户挂载文件系统.</span><br><span class="line">	nouser - 只能被 root 挂载。</span><br><span class="line">	owner - 允许设备所有者挂载.</span><br><span class="line">	sync - I/O 同步进行。</span><br><span class="line">	async - I/O 异步进行。</span><br><span class="line">	dev - 解析文件系统上的块特殊设备。</span><br><span class="line">	nodev - 不解析文件系统上的块特殊设备。</span><br><span class="line">	suid - 允许 suid 操作和设定 sgid 位。这一参数通常用于一些特殊任务，使一般用户运行程序时临时提升权限。</span><br><span class="line">	nosuid - 禁止 suid 操作和设定 sgid 位。</span><br><span class="line">	noatime - 不更新文件系统上 inode 访问记录，可以提升性能。</span><br><span class="line">	nodiratime - 不更新文件系统上的目录 inode 访问记录，可以提升性能(参见 atime 参数)。</span><br><span class="line">	relatime - 实时更新 inode access 记录。只有在记录中的访问时间早于当前访问才会被更新。（与 noatime 相似，但不会打断如 	mutt 或其它程序探测文件在上次访问后是否被修改的进程。），可以提升性能。</span><br><span class="line">	flush - vfat 的选项，更频繁的刷新数据，复制对话框或进度条在全部数据都写入后才消失。</span><br><span class="line">	defaults - 使用文件系统的默认挂载参数，例如 ext4 的默认参数为:rw, suid, dev, exec, auto, nouser, async.</span><br><span class="line"></span><br><span class="line">&lt;dump&gt;  dump 工具通过它决定何时作备份. dump 会检查其内容，并用数字来决定是否对这个文件系统进行备份。 允许的数字是 0 和 1 。0 表示忽略， 1 则进行备份。大部分的用户是没有安装 dump 的 ，对他们而言 &lt;dump&gt; 应设为 0。</span><br><span class="line"></span><br><span class="line">&lt;pass&gt; fsck 读取 &lt;pass&gt; 的数值来决定需要检查的文件系统的检查顺序。允许的数字是0, 1, 和2。 根目录应当获得最高的优先权 1, 其它所有需要被检查的设备设置为 2. 0 表示设备不会被 fsck 所检查。</span><br></pre></td></tr></table></figure>

<h1 id="七、写一个脚本实现分区？"><a href="#七、写一个脚本实现分区？" class="headerlink" title="七、写一个脚本实现分区？"></a><font size=3>七、写一个脚本实现分区？</font></h1><p>前面我们已经知道怎么使用fdisk对sd卡进行分区了，那些命令和操作步骤属实繁琐了些，那是不是可以通过shell脚本来实现sd卡分区？</p>
<h2 id="1-运算符"><a href="#1-运算符" class="headerlink" title="1. | 运算符"></a><font size=3>1. | 运算符</font></h2><p>首先我们回顾一下管道运算符——<code>|</code>，它的作用是将前面命令的输出作为后面命令的输入，例如：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ls -s|sort -nr</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240406080752832.png" alt="image-20240406080752832" style="zoom:50%;" />

<p>-s 是file size，-n是numeric-sort，-r是reverse，反转。该命令列出当前目录中的文档(含size)，并把输出送给sort命令作为输入，sort命令按数字递减的顺序把ls的输出排序。</p>
<h2 id="2-dd命令"><a href="#2-dd命令" class="headerlink" title="2. dd命令"></a><font size=3>2. dd命令</font></h2><p>这里还是回顾一下吧，感觉知道之前sd卡分区情况和大小的时候使用dd命令来清空sd卡还是很快的。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">dd if=/dev/zero of=/dev/sdc                 # 完全清除SD卡，使SD卡上所有数据都是0</span><br><span class="line">dd if=/dev/zero of=/dev/sdc bs=256k count=1 # 使SD卡前256k数据都被清除为数据0</span><br></pre></td></tr></table></figure>

<h2 id="3-shell标签"><a href="#3-shell标签" class="headerlink" title="3. shell标签"></a><font size=3>3. shell标签</font></h2><p>前面我们学习重定向的时候有了解过shell的标签功能：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">cat &lt;&lt; tag</span><br><span class="line">&quot;1&quot;</span><br><span class="line">&quot;2&quot;</span><br><span class="line">&quot;3&quot;</span><br><span class="line">tag</span><br></pre></td></tr></table></figure>

<p>上面的脚本就是将开始标记 tag 和结束标记 tag 之间的内容作为输入，执行结果如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-09-%E5%AD%98%E5%82%A8%E4%BB%8B%E8%B4%A8%E7%AE%A1%E7%90%86%E5%91%BD%E4%BB%A4/img/image-20240406085255966.png" alt="image-20240406085255966" style="zoom:50%;" />

<h2 id="4-脚本实现分区"><a href="#4-脚本实现分区" class="headerlink" title="4. 脚本实现分区"></a><font size=3>4. 脚本实现分区</font></h2><details class="folding-tag" blue><summary> 点击自动分区脚本 </summary>
              <div class='content'>
              <figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">* =====================================================</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">* Copyright © hk. 2022-2025. All rights reserved.</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">* File name  : sd_fdisk.sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">* Author     : 苏木</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">* Date       : 2024-04-06</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">* ======================================================</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="comment">#</span></span></span><br><span class="line"></span><br><span class="line">device=/dev/sdd</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">execute执行语句成功与否打印</span></span><br><span class="line">execute ()</span><br><span class="line">&#123;</span><br><span class="line">    $* &gt;/dev/null</span><br><span class="line">    if [ $? -ne 0 ]; then</span><br><span class="line">        echo</span><br><span class="line">        echo &quot;错误: 执行 $*&quot;</span><br><span class="line">        echo</span><br><span class="line">        exit 1</span><br><span class="line">    fi</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">判断选择的块设备是否存在及是否是一个块设备</span></span><br><span class="line">if [ ! -b $&#123;device&#125; ]; then</span><br><span class="line">    echo &quot;错误: $&#123;device&#125; 不是一个块设备文件&quot;</span><br><span class="line">    exit 1</span><br><span class="line">fi</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">这里防止选错设备，否则会影响Ubuntu系统的启动</span></span><br><span class="line">if [ $&#123;device&#125; = &#x27;/dev/sda&#x27; ];then</span><br><span class="line">    echo &quot;请不要选择sda设备，/dev/sda通常是您的Ubuntu硬盘!继续操作你的系统将会受到影响！脚本已自动退出&quot;</span><br><span class="line">    exit 1 </span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">echo &quot;即将进行对SD卡进行格式化和分区相关操作!&quot;</span><br><span class="line">echo &quot;************************************************************&quot;</span><br><span class="line">echo &quot;*         注意：这将会清除$&#123;device&#125;所有的数据               *&quot;</span><br><span class="line">echo &quot;*         在脚本执行时请不要将$&#123;device&#125;拔出                 *&quot;</span><br><span class="line">echo &quot;*             请按&lt;Enter&gt;确认继续                          *&quot;</span><br><span class="line">echo &quot;************************************************************&quot;</span><br><span class="line">read enter</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">格式化前要卸载，需要注意的是如果之前没有分区，这里可能会报个错，说找不到文件之类的，就不用管</span></span><br><span class="line">for i in `ls -1 $&#123;device&#125;?`; do </span><br><span class="line">    echo &quot;卸载 device &#x27;$i&#x27;&quot;</span><br><span class="line">    umount $&#123;i&#125; 2&gt;/dev/null</span><br><span class="line">done</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">执行格式化</span></span><br><span class="line">execute &quot;dd if=/dev/zero of=$&#123;device&#125; bs=1024 count=1024&quot;</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">第一个分区为64M用来存放设备树与内核镜像文件，因为设备树与内核都比较小，不需要太大的空间</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">第二个分区为SD卡的总大小-64M，用来存放文件系统</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">通过shell的标签传递参数，注意这里的空行都是enter按键，不可删除</span></span><br><span class="line">cat &lt;&lt; END | fdisk -H 255 -S 63 $device</span><br><span class="line">n</span><br><span class="line">p</span><br><span class="line">1</span><br><span class="line"></span><br><span class="line">+64M</span><br><span class="line">n</span><br><span class="line">p</span><br><span class="line">2</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">t</span><br><span class="line">1</span><br><span class="line">c</span><br><span class="line">a</span><br><span class="line">1</span><br><span class="line">w</span><br><span class="line">END</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">通过<span class="built_in">echo</span>命令完成fdisk的命令窗口分区操作</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="built_in">echo</span> <span class="string">&quot;n</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="string">p</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="string">1</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="string"></span></span></span><br><span class="line"><span class="string"><span class="language-bash">#+64M</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="string">n</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="string">p</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="string">2</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="string"></span></span></span><br><span class="line"><span class="string"><span class="language-bash">#</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="string">t</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="string">1</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="string">c</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="string">a</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="string">1</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="string">w</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="string">&quot;</span> | fdisk -H 255 -S 63 <span class="variable">$&#123;device&#125;</span></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">两个分区处理</span></span><br><span class="line">PARTITION1=$&#123;device&#125;1</span><br><span class="line">if [ ! -b $&#123;PARTITION1&#125; ]; then</span><br><span class="line">        PARTITION1=$&#123;device&#125;1</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">PARTITION2=$&#123;device&#125;2</span><br><span class="line">if [ ! -b $&#123;PARTITION2&#125; ]; then</span><br><span class="line">        PARTITION2=$&#123;device&#125;2</span><br><span class="line">fi</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">第一个分区创建为Fat32格式</span></span><br><span class="line">echo &quot;格式化 $&#123;device&#125;1 ...&quot;</span><br><span class="line">if [ -b $&#123;PARTITION1&#125; ]; then</span><br><span class="line">	mkfs.vfat -F 32 -n &quot;boot&quot; $&#123;PARTITION1&#125;</span><br><span class="line">else</span><br><span class="line">	echo &quot;错误: /dev下找不到 SD卡 boot分区&quot;</span><br><span class="line">fi</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">第二个分区创建为ext3格式</span></span><br><span class="line">echo &quot;格式化$&#123;device&#125;2 ...&quot;</span><br><span class="line">if [ -b $&#123;PARITION2&#125; ]; then</span><br><span class="line">	mkfs.ext3 -F -L &quot;rootfs&quot; $&#123;PARTITION2&#125;</span><br><span class="line">else</span><br><span class="line">	echo &quot;错误: /dev下找不到 SD卡 rootfs分区&quot;</span><br><span class="line">fi</span><br></pre></td></tr></table></figure>
              </div>
            </details>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/afe014f2.html">LV02-02-shell-09-存储介质管理命令</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2024年04月06日 - 10:38</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/afe014f2.html" title="LV02-02-shell-09-存储介质管理命令">https://sumumm.github.io/post/afe014f2.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/" rel="tag"><i class="fa fa-tag"></i> LV02-编译基础</a>
              <a href="/tags/shell%E8%84%9A%E6%9C%AC/" rel="tag"><i class="fa fa-tag"></i> shell脚本</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/cc6b299.html" rel="prev" title="城市夜景">
                  <i class="fa fa-angle-left"></i> 城市夜景
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/35275b5a.html" rel="next" title="LV02-NexT-07-加密笔记内容">
                  LV02-NexT-07-加密笔记内容 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
